package com.zuoyeben123.testzybhtml;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zuoyeben123.testzybhtml.socket.SocketTransceiver;
import com.zuoyeben123.testzybhtml.socket.TcpClient;
import com.zuoyeben123.testzybhtml.utils.ImageUtils;

/**
 * Created by wangliang on 2017/11/24.
 */

public class TestDisplayTpActivity extends AppCompatActivity {

    private static final int STATUS_CONNECT_SUCC = 1;
    private static final int STATUS_CONNECT_FAILED = 2;
    private static final int STATUS_CONNECT_IDEL = 0;

    private static final int TP_SEND = 100;
    private static final int TP_INTERVAL = 5000;

    private int mStatus = 0;
    private ImageView tpView;
    private TextView mTvStatus;




    private TcpClient client = new TcpClient() {
        @Override
        public void onConnect(SocketTransceiver transceiver) {
            mStatus = STATUS_CONNECT_SUCC;
            refreshStatus();
        }

        @Override
        public void onConnectFailed() {
            mStatus = STATUS_CONNECT_FAILED;
            refreshStatus();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(TestDisplayTpActivity.this, "连接创建失败", Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onReceive(SocketTransceiver transceiver, final byte[] data) {
//            Log.d("WTest", "receive msg:" + Arrays.toString(data));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tpView.setImageBitmap(ImageUtils.byteToBitmap(data));
                }
            });

        }

        @Override
        public void onDisconnect(SocketTransceiver transceiver) {
            mStatus = STATUS_CONNECT_FAILED;
            refreshStatus();
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_display_tp);

        mTvStatus = findViewById(R.id.tv_status);
        tpView = findViewById(R.id.display_view);

        connect();

    }


    /**
     * 设置IP和端口地址,连接或断开
     */
    private void connect() {
        if (client.isConnected()) {
            // 断开连接
            client.disconnect();
        } else {
            try {
                String hostIP = "192.168.20.45";
                int port = Integer.parseInt("10001");
                client.connect(hostIP, port);
            } catch (NumberFormatException e) {
                Toast.makeText(this, "端口错误", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        }
    }

    private void refreshStatus() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(mStatus == STATUS_CONNECT_SUCC) {
                    mTvStatus.setText("连接创建成功");
                } else if(mStatus == STATUS_CONNECT_FAILED) {
                    mTvStatus.setText("连接已经断开");
                } else {
                    mTvStatus.setText("IDEL");
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(client != null) {
            client.disconnect();
        }
    }


}
